package com.example.reagent_management.ui.operatedialog.dialogs

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.*
import com.example.lib_base.adapter.ItemClickListener
import com.example.lib_base.base.BaseDBDialogFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.lib_base.utils.onQueryTextFlow
import com.example.reagent_management.BR
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentStoreChooseBinding
import com.example.reagent_management.room.entity.ReagentInformation
import com.example.reagent_management.ui.operatedialog.adapter.StorePageAdapter
import com.example.reagent_management.ui.operatedialog.viewmodle.StoreChooseViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

/*
* 仅用于出入库操作时弹出来给选择的 弹窗  先不支持多条件查询，先支持插入
*
* 库存的基本信息操作移步到 "试剂管理"  viewModels一个辅助类  里面包装了viewModel的工厂
* */
//关于hilt  一个注入框架 google官方也有介绍

// //dialog中显示dialog 因为dimAmout原因会显示原背景闪一下，这里移除相关flag
//可以在第二个dialog中设置window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
@AndroidEntryPoint
class StoreChooseFragment : BaseDBDialogFragment() {

    private val mState: StoreChooseViewModel by viewModels({ requireParentFragment() })
    private lateinit var job: Job
    private lateinit var myAdapter: StorePageAdapter

    override fun getDataBindingConfig(): DataBindingConfig? {
        myAdapter = StorePageAdapter()
        job = mState.getAllReagent("")
        myAdapter.setOnItemClick(object : ItemClickListener {
            override fun itemClick(data: Any?) {
                (data as ReagentInformation).let {
                    this@StoreChooseFragment.updateData(data)
                    dismissAllowingStateLoss()
                }
            }
        })
        mState.reagentMessages.observe(this@StoreChooseFragment, Observer {
            myAdapter.submitData(this@StoreChooseFragment.lifecycle, it)
        })
        return DataBindingConfig(R.layout.fragment_store_choose, BR.vm, mState)
            .addBindingParam(BR.adapter, myAdapter)
    }

    @SuppressLint("CheckResult")
    override fun initViewOrObserve() {
        setTransAndHeight(true, 0.5f, true)
        (getBinding() as FragmentStoreChooseBinding).apply {
            no.setOnClickListener {
                dismissAllowingStateLoss()
            }
            //试剂信息窗口  代码已舍弃 耦合代码不好看 当然也可写个kt文件分离出来
            //新增试剂
            ok.setOnClickListener {
                ReagentInsertFragment().showNow(childFragmentManager, "ReagentFragment")
            }
            search.setOnSearchClickListener { title.visibility = View.INVISIBLE }
            search.setOnCloseListener {
                title.visibility = View.VISIBLE
                false
            }
            lifecycleScope.launch {
                search.onQueryTextFlow()
                    .debounce(500)
                    .distinctUntilChanged()
                    .flowOn(Dispatchers.IO)
                    .collect {
                        if (job.isActive) job.cancel()
                        if (job.isCancelled) {
                            job = mState.getAllReagent(it)
                        }
                    }
            }
        }
    }


}